package com.ruoyi.gp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.gp.domain.bo.OptionsBo;
import com.ruoyi.gp.domain.vo.OptionsVo;
import com.ruoyi.gp.domain.Options;
import com.ruoyi.gp.mapper.OptionsMapper;
import com.ruoyi.gp.service.IOptionsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 问题选项Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-08
 */
@RequiredArgsConstructor
@Service
public class OptionsServiceImpl implements IOptionsService {

    private final OptionsMapper baseMapper;

    /**
     * 查询问题选项
     */
    @Override
    public OptionsVo queryById(Long optionId){
        return baseMapper.selectVoById(optionId);
    }

    /**
     * 查询问题选项列表
     */
    @Override
    public TableDataInfo<OptionsVo> queryPageList(OptionsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Options> lqw = buildQueryWrapper(bo);
        Page<OptionsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询问题选项列表
     */
    @Override
    public List<OptionsVo> queryList(OptionsBo bo) {
        LambdaQueryWrapper<Options> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Options> buildQueryWrapper(OptionsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Options> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getQuestionId() != null, Options::getQuestionId, bo.getQuestionId());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), Options::getContent, bo.getContent());
        return lqw;
    }

    /**
     * 新增问题选项
     */
    @Override
    public Boolean insertByBo(OptionsBo bo) {
        Options add = BeanUtil.toBean(bo, Options.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setOptionId(add.getOptionId());
        }
        return flag;
    }

    /**
     * 修改问题选项
     */
    @Override
    public Boolean updateByBo(OptionsBo bo) {
        Options update = BeanUtil.toBean(bo, Options.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Options entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除问题选项
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
